package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;
	/**
	 * 查询所有分类（1、2、3级分类）
	 *
	 * @return 业务数据：[{"categoryId":1,"categoryName":"分类",categoryChild:[..]},{其他1级分类}]
	 */
    @Override
    public List<JSONObject> getBaseCategoryList() {
		//封装一类分类对象
		ArrayList<JSONObject> returnList = new ArrayList<>();
		//查询所有分类数据，查询视图
		List<BaseCategoryView> allCategoryList = baseCategoryViewMapper.selectList(null);
		//3.处理一级分类数据
		//3.1 对所有分类集合列表进行分组按照1级分类ID进行分组 得到Map<分组ID，一级分类列表>
		Map<Long, List<BaseCategoryView>> map1 = allCategoryList
				.stream()
				.collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		for (Map.Entry<Long, List<BaseCategoryView>> entry1 : map1.entrySet()) {
			//3.2.1 创建"一级分类JSON对象"
			JSONObject jsonObject1 = new JSONObject();
			//3.2.2 封装1级分类ID
			jsonObject1.put("categoryId", entry1.getKey());
			//3.2.3 封装1级分类名称
			jsonObject1.put("categoryName", entry1.getValue().get(0).getCategory1Name());
			//4.处理2级分类数据
			List<JSONObject> jsonObject2List = new ArrayList<>();
			//4.1 获取分组后"1级"分类列表，按照2级分类ID进行分组，得到二级分类Map<2级分类ID，"2级分类"列表>
			Map<Long, List<BaseCategoryView>> map2 = entry1.getValue()
					.stream()
					.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//4.2 声明保存2级分类JSON对象集合

			//4.3 遍历二级分类Map<2级分类ID，"2级分类"列表> 封装2级分类JSON对象
			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : map2.entrySet()) {
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId", entry2.getKey());
				jsonObject2.put("categoryName", entry2.getValue().get(0).getCategory2Name());
				jsonObject2List.add(jsonObject2);
				//5.处理3级分类数据
				//5.1 声明保存3级分类JSON对象集合
				List<JSONObject> jsonObject3List = new ArrayList<>();
				for (BaseCategoryView baseCategoryView : entry2.getValue()){
					//5.2 遍历分组后"2级"分类列表，构建3级分类JSON对象
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
					jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
					jsonObject3List.add(jsonObject3);
				}
				//5.3 将当前2级分类下包含3级分类集合放入到"二级分类JSON对象""categoryChild"属性中
				jsonObject2.put("categoryChild", jsonObject3List);
			}
			//4.3 将当前1及分类下包含2级分类集合放入到"一级分类JSON对象""categoryChild"属性中
			jsonObject1.put("categoryChild", jsonObject2List);
			//3.2.4 将1级分类JSON对象加入到最终结果集合中
			returnList.add(jsonObject1);
		}
		return returnList;
    }

	@Override
	public List<BaseAttribute> getAttributesByCategory1Id(Long category1Id) {
		//1.获取持久层接口，调用持久层动态SQL
		return baseAttributeMapper.getAttributesByCategory1Id(category1Id);
	}

    @Override
    public BaseCategoryView getCategoryView(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);
    }

	/**
	 * 根据1级分类ID查询置顶3级分类列表
	 *
	 * @param category1Id
	 * @return
	 */
    @Override
    public List<BaseCategory3> getTopBaseCategory3(Long category1Id) {
		//根据一级分类ID查询所有2级分类列表
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(
				new LambdaQueryWrapper<BaseCategory2>()
						.eq(BaseCategory2::getCategory1Id, category1Id)
						.select(BaseCategory2::getId)
		);
		if (CollUtil.isNotEmpty(baseCategory2List)){
			List<Long> baseCategory2IdList = baseCategory2List
					.stream()
					.map(BaseCategory2::getId)
					.collect(Collectors.toList());
			//根据2级分类ID查询置顶的7个3级分类列表
			LambdaQueryWrapper<BaseCategory3> baseCategory3LambdaQueryWrapper = new LambdaQueryWrapper<BaseCategory3>()
					.eq(BaseCategory3::getIsTop, 1)
					.in(BaseCategory3::getCategory2Id, baseCategory2IdList)
					.select(BaseCategory3::getId, BaseCategory3::getName, BaseCategory3::getCategory2Id)
					.last("LIMIT 7")
					.orderByAsc(BaseCategory3::getOrderNum);
			//2.3 查询得到7个置顶三级分类
			List<BaseCategory3> list = baseCategory3Mapper.selectList(baseCategory3LambdaQueryWrapper);
			return list;
		}


        return List.of();
    }

    @Override
    public JSONObject getBaseCategoryListByCategory1Id(Long category1Id) {
		//1.根据1级分类ID查询分类视图得到“1级”分类列表  封装1级分类对象
		//1.1 封装1级分类对象
		JSONObject jsonObject1 = new JSONObject();
		List<BaseCategoryView> baseCategory1ViewList = baseCategoryViewMapper.selectList(
				new LambdaQueryWrapper<BaseCategoryView>()
						.eq(BaseCategoryView::getCategory1Id, category1Id)
		);
		if (CollUtil.isNotEmpty(baseCategory1ViewList)){
			jsonObject1.put("categoryId", baseCategory1ViewList.get(0).getCategory1Id());
			jsonObject1.put("categoryName", baseCategory1ViewList.get(0).getCategory1Name());
		}

		//2.处理二级分类
		// 2.1 对"1级"分类列表进行按2级分类ID分组
		List<JSONObject> jsonObject2List = new ArrayList<>();
		Map<Long, List<BaseCategoryView>> map2 = baseCategory1ViewList.stream()
				.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		//2.2 遍历"2级"分类Map
		map2.forEach((category2Id, baseCategoryViewList) -> {
			JSONObject jsonObject2 = new JSONObject();
			jsonObject2.put("categoryId", baseCategoryViewList.get(0).getCategory2Id());
			jsonObject2.put("categoryName", baseCategoryViewList.get(0).getCategory2Name());
			jsonObject2List.add(jsonObject2);
			//2.3 封装2级分类JSON对象

			//2.4 将2级分类集合存入1级分类categoryChild中
			jsonObject1.put("categoryChild", jsonObject2List);

			//3.处理三级分类
			List<JSONObject> jsonObject3List = new ArrayList<>();
			for (BaseCategoryView baseCategoryView :baseCategoryViewList){
				JSONObject jsonObject3 = new JSONObject();
				jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
				jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
				jsonObject3List.add(jsonObject3);
			}
			jsonObject2.put("categoryChild", jsonObject3List);
			jsonObject2List.add(jsonObject2);
		});
		return jsonObject1;

	}

}
